/*
 * Created by JFormDesigner on Wed May 26 19:59:17 CST 2021
 */

package renthouse.view.information.uesr;

import renthouse.dao.HouseDao;
import renthouse.entry.House;
import renthouse.view.mainManger.UserFrame;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;

/**
 * @author Brainrain
 */
public class UserFindHouse extends JFrame {
    public static void main(String[] args) {
        UserFindHouse userFindHouse=new UserFindHouse();
        userFindHouse.setVisible(true);
    }
    public UserFindHouse() {
        initComponents();
        myTableInit();
    }

    private void menuIteminsert1ActionPerformed() {
        // TODO add your code here
        int selectRow = table1.getSelectedRow(); //如果得到的值是-1 表示没有选中行
        if (selectRow == -1) {
            JOptionPane.showMessageDialog(null, "请选择您需要修改的数据");
            return;
        }
        Object houseId= table1.getValueAt(selectRow,0);

        UserAddOrder userAddOrder=new UserAddOrder((Integer) houseId);
userAddOrder.setVisible(true);

        myTableInit();

    }

    HouseDao houseDao = new HouseDao();
    public static int pageNum = 1;
    public static int pageSize = 3;
    public static int allPage = 0;
    public static int allCount = 0;

    public void myTableInit() {
        House house = new House();
        String housekeyVal = houseKey.getText();
        String housekey = (String) Housekey.getSelectedItem();
        if (housekeyVal != null && !"".equals(housekeyVal)) {
            if (housekey=="房屋状态") {//按照不同条件查询房屋
                house.setState(housekeyVal);
            } else if (housekey=="房屋风格") {
                house.setStyle(housekeyVal);
            } else if (housekey=="房屋租金") {
                house.setRent(Integer.valueOf(housekeyVal));
            }
        }
        String[] ss = new String[]{"房屋id", "房东id", "地址", "状态", "风格", "租金"};

        List <House> list = houseDao.queryForPage(house, pageNum, pageSize);
        int len = 0;
        if (list != null || list.size() > 0) {
            len = list.size();
        }
        Object[][] objects = new Object[len][6];//定义每列的值
        for (int i = 0; i < list.size(); i++) {
            House house1 = list.get(i);
            Object[] objects1 = new Object[]{house1.getHouseid(),
                    house1.getMasterid(), house1.getAddress(),
                    house1.getState(), house1.getStyle(), house1.getRent()};
            objects[i] = objects1;
        }
        table1.setModel(new DefaultTableModel(objects, ss));

        table1.getColumnModel().getColumn(0).setMaxWidth(50); //设置宽度
        table1.getColumnModel().getColumn(1).setMaxWidth(50); //设置宽度
        table1.getColumnModel().getColumn(2).setMaxWidth(50); //设置宽度
        table1.getColumnModel().getColumn(3).setMaxWidth(50); //设置宽度

        allCount = houseDao.queryCount(house);

        if (allCount % pageSize == 0) {
            allPage = allCount / pageSize;
            if (allCount == 0) {
                allPage = 1;
            }
        } else {
            allPage = (allCount / pageSize) + 1;
        }

        label2.setText("第 " + pageNum + " 页/共 " + allPage + " 页   每页 " + pageSize + " 条/共 " + allCount + " 条");
    }

    private void button1ActionPerformed() {
        // TODO add your code here
        pageNum = 1;
        myTableInit();

    }

    private void buttonshangActionPerformed(ActionEvent e) {
        // TODO add your code here
        if (pageNum == 1) {
            JOptionPane.showMessageDialog(null, "已经是第一页了");
            return;
        }
        pageNum--;
        myTableInit();

    }

    private void buttonxiaActionPerformed(ActionEvent e) {
        // TODO add your code here
        if (pageNum == allPage) {
            JOptionPane.showMessageDialog(null, "已经最后一页了");
            return;
        }
        pageNum++;
        myTableInit();

    }

    private void buttonweiActionPerformed(ActionEvent e) {
        // TODO add your code here
        pageNum = allPage;
        myTableInit();
    }

    private void buttonshouActionPerformed() {
        // TODO add your code here
        pageNum = 1;
        myTableInit();
    }

    private void menuItemreturnActionPerformed() {
        // TODO add your code here
        setVisible(false);
        UserFrame userFrame = new UserFrame();
        userFrame.setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        menuBar1 = new JMenuBar();
        menu1 = new JMenu();
        menuIteminsert1 = new JMenuItem();
        menu2 = new JMenu();
        menuItemreturn = new JMenuItem();
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        panel2 = new JPanel();
        buttonshang = new JButton();
        buttonxia = new JButton();
        buttonwei = new JButton();
        label2 = new JLabel();
        buttonshou = new JButton();
        panel3 = new JPanel();
        houseKey = new JTextField();
        button1 = new JButton();
        Housekey = new JComboBox<>();

        //======== this ========
        setTitle("\u623f\u5ba2\u67e5\u8be2\u623f\u5c4b");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== menuBar1 ========
        {

            //======== menu1 ========
            {
                menu1.setText("\u7528\u6237\u7ba1\u7406");

                //---- menuIteminsert1 ----
                menuIteminsert1.setText("\u4e0b\u5355\u79df\u623f");
                menuIteminsert1.addActionListener(e -> menuIteminsert1ActionPerformed());
                menu1.add(menuIteminsert1);
            }
            menuBar1.add(menu1);

            //======== menu2 ========
            {
                menu2.setText("\u5176\u4ed6");

                //---- menuItemreturn ----
                menuItemreturn.setText("\u8fd4\u56de\u4e3b\u83dc\u5355");
                menuItemreturn.addActionListener(e -> menuItemreturnActionPerformed());
                menu2.add(menuItemreturn);
            }
            menuBar1.add(menu2);
        }
        setJMenuBar(menuBar1);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //======== scrollPane1 ========
            {

                //---- table1 ----
                table1.setModel(new DefaultTableModel(
                    new Object[][] {
                        {"1", "2"},
                        {null, null},
                    },
                    new String[] {
                        null, null
                    }
                ));
                scrollPane1.setViewportView(table1);
            }
            panel1.add(scrollPane1);
            scrollPane1.setBounds(0, 50, 795, 360);

            //======== panel2 ========
            {
                panel2.setLayout(null);

                //---- buttonshang ----
                buttonshang.setText("\u4e0a\u4e00\u9875");
                buttonshang.addActionListener(e -> buttonshangActionPerformed(e));
                panel2.add(buttonshang);
                buttonshang.setBounds(new Rectangle(new Point(525, 15), buttonshang.getPreferredSize()));

                //---- buttonxia ----
                buttonxia.setText("\u4e0b\u4e00\u9875");
                buttonxia.addActionListener(e -> buttonxiaActionPerformed(e));
                panel2.add(buttonxia);
                buttonxia.setBounds(600, 15, 80, 25);

                //---- buttonwei ----
                buttonwei.setText("\u5c3e\u9875");
                buttonwei.addActionListener(e -> buttonweiActionPerformed(e));
                panel2.add(buttonwei);
                buttonwei.setBounds(700, 15, 80, 25);

                //---- label2 ----
                label2.setText("\u7b2c 1 \u9875/\u5171 1 \u9875   \u6bcf\u9875 4 \u6761/\u5171 12 \u6761");
                panel2.add(label2);
                label2.setBounds(15, 20, 225, 17);

                //---- buttonshou ----
                buttonshou.setText("\u9996\u9875");
                buttonshou.addActionListener(e -> buttonshouActionPerformed());
                panel2.add(buttonshou);
                buttonshou.setBounds(new Rectangle(new Point(440, 15), buttonshou.getPreferredSize()));

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel2.getComponentCount(); i++) {
                        Rectangle bounds = panel2.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel2.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel2.setMinimumSize(preferredSize);
                    panel2.setPreferredSize(preferredSize);
                }
            }
            panel1.add(panel2);
            panel2.setBounds(5, 420, 785, 50);

            //======== panel3 ========
            {
                panel3.setLayout(null);
                panel3.add(houseKey);
                houseKey.setBounds(100, 5, 225, 24);

                //---- button1 ----
                button1.setText("\u67e5\u8be2");
                button1.addActionListener(e -> button1ActionPerformed());
                panel3.add(button1);
                button1.setBounds(new Rectangle(new Point(360, 5), button1.getPreferredSize()));

                //---- Housekey ----
                Housekey.setModel(new DefaultComboBoxModel<>(new String[] {
                    "\u623f\u5c4b\u72b6\u6001",
                    "\u623f\u5c4b\u98ce\u683c",
                    "\u623f\u5c4b\u79df\u91d1"
                }));
                panel3.add(Housekey);
                Housekey.setBounds(new Rectangle(new Point(10, 5), Housekey.getPreferredSize()));

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel3.getComponentCount(); i++) {
                        Rectangle bounds = panel3.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel3.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel3.setMinimumSize(preferredSize);
                    panel3.setPreferredSize(preferredSize);
                }
            }
            panel1.add(panel3);
            panel3.setBounds(0, 5, 790, 45);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 0, 795, 475);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JMenuBar menuBar1;
    private JMenu menu1;
    private JMenuItem menuIteminsert1;
    private JMenu menu2;
    private JMenuItem menuItemreturn;
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel panel2;
    private JButton buttonshang;
    private JButton buttonxia;
    private JButton buttonwei;
    private JLabel label2;
    private JButton buttonshou;
    private JPanel panel3;
    private JTextField houseKey;
    private JButton button1;
    private JComboBox<String> Housekey;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
